package com.wdz.http;

import org.springframework.util.StringUtils;

import java.io.Serializable;

/**
 * 请求相应实体类
 * 统一用JsonObject做为回调数据的json格式类
 */
public class Response implements Serializable {
    private static final long serialVersionUID = -2561617247257179381L;
    private int status;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    /**
     * 前端响应消息的提示方式 500：弹框提示，501：气泡提示，
     */
    private int code;
    private String message;
    private Object data;

    public Response(){

    }

    public Response(int status, int code, String message, Object data) {
        this.status = status;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 普通不带数据的成功回调方法
     * @return 返回响应体
     */
    public  static Response success(){
        return new Response(0,0,"成功",null);
    }

    /**
     * 普通带数据的成功回调方法
     * @return
     */
    public static Response success(Object data){
        return new Response(0,0,"成功",data);
    }
    /**
     * 普通成功带提示消息
     */
    public static Response success(String message){
        return new Response(0,0, message,null);
    }
    /**
     * 普通成功带提示消息和返回数据
     */
    public static Response success(String message,Object data){
        return new Response(0,0, message,data);
    }
    /**
     * 普通的失败回调
     */
    public static Response error(){
        return new Response(500,500,"失败",null);
    }
    /**
     * 带提示信息的失败回调
     */
    public static Response error(String message){
        return new Response(500,500,message,null);
    }
    /**
     * 带提示信息和提示方式的失败回调
     */
    public static Response error(String message,int code){
        return new Response(500,code,message,null);
    }
    /**
     * 504服务异常失败回调方法
     * @return
     */
    public static Response error504(String message){
        return new Response(500,504,message,null);
    }

    /**
     * 405 用户信息失效的回调方法
     * @param message
     * @return
     */
    public static Response userInvalid(String message){
        return new Response(405,405, message,null);
    }

    /**
     * 401 用户没有权限
     * @param message
     * @return
     */
    public static Response noPermission(String message){
        return new Response(401,401, message,null);
    }

    /**
     * 505请求第三放接口失败回调方法
     * @return
     */
    public static Response error505(){
        return new Response(500,504,"请求第三方接口失败",null);
    }
    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Response{" +
                "status=" + status +
                ", code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    public String toJsonString() {
        if(StringUtils.isEmpty(this.data)){
            this.data = "{}";
        }
        return "{" +
                "\"status\":\"" + status +
                "\", \"code\":\"" + code +
                "\", \"message\":\"" + message +
                "\", \"data\":" + data +
                "}";
    }
}
